<template>
  <el-dialog v-el-drag-dialog :title="!form.resourceId?'新建资源':'编辑资源'" :visible.sync="visible" :append-to-body="appendToBody">
    <el-form ref="resourceForm" :model="form" label-width="120px" :disabled="!hasLoaded">
      <el-card>
        <el-form-item hidden>
          <el-input v-model="form.resourceId" hidden/>
          <el-input v-model="form.fileId" hidden/>
          <el-input v-model="form.extension" hidden/>
          <el-input v-model="form.size" hidden/>
          <el-input v-model="form.duration" hidden/>
        </el-form-item>
        <el-form-item label="资源名" prop="name">
          <el-input v-model="form.name"/>
        </el-form-item>
        <el-form-item label="资源类型" prop="type">
          <el-select v-model="form.type">
            <el-option
              v-for="item in optionOfTypes"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="资源文件" prop="fileId">
          <file-viewer v-model="resourceFiles" :file-data="fileData" :file-size="'500mb'" :limit="1" @getFileData="getFileData" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status">
            <el-option
              v-for="item in optionOfStatus"
              :key="item.dictCode"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-card>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button v-if="hasLoaded" type="primary" icon="el-icon-check" @click="onSubmit">确 定</el-button>
      <el-button icon="el-icon-close" @click="onClose">关 闭</el-button>
    </span>
  </el-dialog>
</template>

<script>
import FileViewer from '@/components/FileViewer'
import { resourceLoad, resourceSave } from '@/api/training/resource'
import { fileManageTable } from '@/api/tool/file'

export default {
  name: 'TrainingResourceFormDialog',
  components: { FileViewer },
  data() {
    return {
      visible: false,
      appendToBody: false,
      hasLoaded: false,
      form: {
        resourceId: undefined,
        name: undefined,
        type: undefined,
        fileId: undefined,
        extension: undefined,
        size: undefined,
        duration: undefined,
        status: '0'
      },
      originFileId: undefined,
      optionOfTypes: [],
      optionOfStatus: [],
      resourceFiles: [],
      fileData: []
    }
  },
  methods: {
    init(id) {
      this.hasLoaded = false
      this.form.resourceId = id || null
      this.visible = true
      this.$nextTick(() => {
        this.loadForm(id)
        this.getDictData()
      })
    },
    loadForm(id) {
      resourceLoad({id: id}).then(response => {
        this.form = this.$api.objectUpdate(this.form, response.param)
        this.originFileId = response.param.fileId
        if (this.form.fileId) {
          this.resourceFiles.push(response.param.fileId)
          this.getFileData()
        }
        // 判断当前用户权限
        this.hasLoaded = this.$perm.hasPermissionOr(['training:resource:edit'])
      }).catch(() => {
      })
    },
    getDictData() {
      const that = this
      this.$http.all([this.$dictGroup('sys_available_type'), this.$dictGroup('sys_file_type')])
        .then(this.$http.spread(function (status, types) {
          that.optionOfStatus = status
          that.optionOfTypes = types
        }))
    },
    getFileData() {
      fileManageTable({Q_fileId_S_IN: this.resourceFiles.join(',')}).then(response => {
        if (response.param.data && response.param.data.length > 0) {
          this.fileData = response.param.data
        }
      })
    },
    onSubmit() {
      if (this.resourceFiles.length === 0) {
        this.$modal.msgError('请先上传资源附件')
        return
      }
      this.form.fileId = this.resourceFiles[0]
      if (this.form.fileId !== this.originFileId) {
        this.form.extension = undefined
        this.form.size = undefined
        this.form.duration = undefined
      }
      this.$refs.resourceForm.validate(valid => {
        if (valid) {
          resourceSave(this.form).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.$emit('confirmForm')
            this.onClose()
          }).catch(() => {
          })
        }
      })
    },
    onClose() {
      this.$refs.resourceForm.resetFields()
      this.originFileId = undefined
      this.resourceFiles = []
      this.fileData = []
      this.visible = false
    }
  }
}
</script>
